import {
    getCqlfx,
    getCqlph,
    getJblxfx,
    getJbyrph,
    getQjlxfx,
    getQjryph,
    getYcfx,
    getYcph
} from '@/services/attendance/reportService';

export default {
    namespace: 'report',
    state: {
        cqlfx: {},
        cqlph: [],
        qjlxfx: [],
        qjryph: [],
        jblxfx: {},
        jbryph: [],
        yclxfx: {},
        ycryph: []
    },
    effects: {
        *fetchData({ payload }, { put, all }) {
            const {
                qjryTypeCode,
                ycryTypeCode,
                jbryTypeCode,
                ...rest
            } = payload;
            yield all([
                put({
                    type: 'fetchCqlfx',
                    payload: rest
                }),
                put({
                    type: 'fetchCqlph',
                    payload: {
                        isLookMore: '',
                        ...rest
                    }
                }),
                put({
                    type: 'fetchQjlxfx',
                    payload: rest
                }),
                put({
                    type: 'fetchQjryph',
                    payload: {
                        typeCode: qjryTypeCode,
                        ...rest
                    }
                }),
                put({
                    type: 'fetchJblxfx',
                    payload: rest
                }),
                put({
                    type: 'fetchJbryph',
                    payload: {
                        typeCode: jbryTypeCode,
                        ...rest
                    }
                }),
                put({
                    type: 'fetchYclxfx',
                    payload
                }),
                put({
                    type: 'fetchYcryph',
                    payload: {
                        typeCode: ycryTypeCode,
                        ...rest
                    }
                })
            ]);
        },
        *fetchCqlfx({ payload }, { put, call }) {
            const result = yield call(getCqlfx, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        cqlfx: result.data
                    }
                });
            }
        },
        *fetchCqlph({ payload }, { put, call }) {
            const result = yield call(getCqlph, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        cqlph: result.data.rows
                    }
                });
            }
        },
        *fetchQjlxfx({ payload }, { put, call }) {
            const result = yield call(getQjlxfx, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        qjlxfx: result.data
                    }
                });
            }
        },
        *fetchQjryph({ payload, callback }, { put, call }) {
            const result = yield call(getQjryph, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        qjryph: result.data
                    }
                });
                if (callback) callback();
            }
        },
        *fetchJblxfx({ payload }, { put, call }) {
            const result = yield call(getJblxfx, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        jblxfx: result.data
                    }
                });
            }
        },
        *fetchJbryph({ payload, callback }, { put, call }) {
            const result = yield call(getJbyrph, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        jbryph: result.data
                    }
                });
                if (callback) callback();
            }
        },
        *fetchYclxfx({ payload }, { put, call }) {
            const result = yield call(getYcfx, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        yclxfx: result.data
                    }
                });
            }
        },
        *fetchYcryph({ payload, callback }, { put, call }) {
            const result = yield call(getYcph, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        ycryph: result.data
                    }
                });
                if (callback) callback();
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear() {
            return {
                cqlfx: {},
                cqlph: [],
                qjlxfx: [],
                qjryph: [],
                jblxfx: {},
                jbryph: [],
                yclxfx: {},
                ycryph: []
            };
        }
    }
};
